package top.leix.springdemo.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.leix.springdemo.Res;
import top.leix.springdemo.Utils.TokenUtil;
import top.leix.springdemo.mapper.AdministratorMapper;
import top.leix.springdemo.mapper.LabAssistantMapper;
import top.leix.springdemo.mapper.StudentMapper;
import top.leix.springdemo.mapper.TeacherMapper;
import top.leix.springdemo.pojo.Administrator;
import top.leix.springdemo.pojo.LabAssistant;
import top.leix.springdemo.pojo.Student;
import top.leix.springdemo.pojo.Teacher;
import top.leix.springdemo.vo.StudentVO;

import javax.annotation.Resource;

@Service
public class LoginService {
    @Resource
    AdministratorMapper administratorMapper;
    @Resource
    TeacherMapper teacherMapper;
    @Resource
    StudentMapper studentMapper;
    @Resource
    LabAssistantMapper labAssistantMapper;
    public Res loginAdmin(String userAccount, String userPassword) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        queryWrapper.eq("userPassword",userPassword);
        Administrator administrator = administratorMapper.selectOne(queryWrapper);
        if(administrator != null){
            System.out.println("登录成功");
            return Res.ok(administrator.getUserName());
        }
        System.out.println("账号或密码错误");
        return Res.failed("账号或密码错误");
        }

    public Res loginTeacher(String userAccount, String userPassword) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        queryWrapper.eq("userPassword",userPassword);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if(teacher != null){
            System.out.println("登录成功");
            return Res.ok(teacher.getUserName());
        }
        System.out.println("账号或密码错误");
        return Res.failed("账号或密码错误");
    }
    public Res loginStudent(String userAccount, String userPassword) {
        System.out.println("账号是："+ userAccount);
        System.out.println("密码是："+ userPassword);
        if(userAccount == null
                || userPassword == null
                || StrUtil.isEmpty(userAccount)
                || StrUtil.isEmpty(userPassword)){
            return Res.failed("请输入账号和密码！");
        }
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        queryWrapper.eq("userPassword",userPassword);
        Student student = studentMapper.selectOne(queryWrapper);
        if(student != null){
            System.out.println("登录成功");
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO); //转换成没有password的VO
            //生成Token
            String token = TokenUtil
                    .createToken(student.getUserAccount(), student.getUserPassword());
            studentVO.setToken(token);
            return Res.ok(studentVO);
        }
        System.out.println("账号或密码错误");
        return Res.failed("账号或密码错误");
    }
    public Res loginLabAssistant(String userAccount, String userPassword) {
        QueryWrapper<LabAssistant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        queryWrapper.eq("userPassword",userPassword);
        LabAssistant labAssistant = labAssistantMapper.selectOne(queryWrapper);
        if(labAssistant != null){
            System.out.println("登录成功");
            return Res.ok(labAssistant.getUserName());
        }
        System.out.println("账号或密码错误");
        return Res.failed("账号或密码错误");
    }

    public Student queryStudentByAccount(String userAccount) {

        return studentMapper.selectOne(new QueryWrapper<Student>().eq("userAccount",userAccount));
    }
        
}

